package org.dna.metronomo.persistence;

import java.io.Serializable;

/**
 * Represent a time period in term of days, hours, minutes.
 * */
public class TimePeriod implements Serializable {
	private static final long serialVersionUID = 8022326494055308685L;
	private int days;
	private int hours;
	private int minutes;
	
	/**
	 * Constructor with range checking.
	 * 
	 * @throws IllegalArgumentException if hours not in [0..23] and if minutes 
	 * not in [0..59].
	 * */
	public TimePeriod(int days, int hours, int minutes) {
		checkHours(hours);
		checkMinutes(minutes);
		this.days = days;
		this.hours = hours;
		this.minutes = minutes;
	}
	
	public TimePeriod() {}
	
	public int getDays() {
		return days;
	}

	public void setDays(int days) {
		this.days = days;
	}

	public int getHours() {
		return hours;
	}

	/**
	 * @throws IllegalArgumentException if hours not in [0..23].
	 * */
	public void setHours(int hours) {
		checkHours(hours);
		this.hours = hours;
	}

	private void checkHours(int hours) {
		if (!(0 <= hours && hours <= 23))
			throw new IllegalArgumentException("hours must be in the range [0..23]");
	}

	public int getMinutes() {
		return minutes;
	}

	/**
	 * @throws IllegalArgumentException if minutes not in [0..59].
	 * */
	public void setMinutes(int minutes) {
		checkMinutes(minutes);
		this.minutes = minutes;
	}

	private void checkMinutes(int minutes) {
		if (!(0 <= minutes && minutes <= 59))
			throw new IllegalArgumentException("minutes must be in the range [0..59]");
	}
	
	@Override
	public String toString() {
		StringBuilder durationSb = new StringBuilder();
		if (days > 0)
			durationSb.append(days).append("d ");
		if (hours > 0)
			durationSb.append(hours).append("h ");
		if (minutes > 0)
			durationSb.append(minutes).append("m ");
		return durationSb.toString();
	}

}
